import 'package:flutter/material.dart';

class AppLifePage2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AppLifePageState();
  }
}

class _AppLifePageState extends State<AppLifePage2> {
  late AppLifecycleListener _appLifecycleListener;
  @override
  void initState() {
    super.initState();

    _appLifecycleListener = AppLifecycleListener(
      onShow: () {
        print('onShow 应用显示');
      },
      onHide: () {
        print('onHide 应用隐藏');
      },
      onResume: () {
        print('onResume 应用恢复');
      },
      onInactive: () {
        print('onInactive 应用 非活跃');
      },
      onPause: () {
        print('onPause 应用暂停');
      },
      onDetach: () {
        print('onDetach 应用分享');
      },
      onRestart: () {
        print('onDetach 应用重启');
      },
      onStateChange: (state) {
        print('onStateChange 状态改变 $state');
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    _appLifecycleListener.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('生命周期页面')),
      body: SizedBox.expand(
        child: Container(
          color: Colors.purple[100],
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                OutlinedButton(
                  onPressed: () {
                    a = 4000;
                    setState(() {});
                  },
                  child: Text('刷新'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  int a = 2000;
}
